<?php

/*------------------------------------------------
*
*   Explay Engine Content Management System.
*   Copyright © 2008 Golovdinov Alexander.
*
*-------------------------------------------------
*
*   Official site: wwww.alex-home.spb.ru/explay.html
*   Contact e-mail: golovdinov@gmail.com
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
------------------------------------------------*/


header ("Cache-control: no-cache");

define ('EXPLAY', 1);

ob_start(); 
ob_implicit_flush(0); 

$start_time = microtime(); 
$start_array = explode(' ',$start_time); 
$start_time = $start_array[1] + $start_array[0];

// Подключаем главные файлы
include $_SERVER['DOCUMENT_ROOT'].'/engine/functions.php';       // Основное API
include $_SERVER['DOCUMENT_ROOT'].'/engine/settings.php';        // Настроки сайта
include $_SERVER['DOCUMENT_ROOT'].'/engine/mysql.class.php';     // БД
include $_SERVER['DOCUMENT_ROOT'].'/engine/user.class.php';      // Объект пользователя
include $_SERVER['DOCUMENT_ROOT'].'/engine/connect.php';         // Подключение к БД и начальная инициализация

mysql_query ("UPDATE ".DB_PEREFIX."_sessions SET page = 'Админпанель' WHERE sesid = '$unical_sesid'");

$theme = 'admin';
include $_SERVER['DOCUMENT_ROOT'].'/themes/admin/main.php';

// REGISTRATION
$login_ = addslashes($_POST['login']);
$pass_ = expl_hash($_POST['pass']);
$remember = $_POST['remember'];

if (isset($_POST['remember']))
	$time = time() + ($SITE['cookie_live'] * 86400);
else
	$time = time() + 7200;
	
	

if ($_GET['admin_mod'] == 'enter') {
	if ($login_ == '' || $pass_ == '') print_big_authorization ('Вход с таким логином и паролем не возможен', 'index.php');
	else {
		$get_login = $db->query ("SELECT * FROM ".DB_PEREFIX."_users WHERE user_login = '$login_' && user_password = '$pass_' && user_group = 'admin'");
		
		if ($db->num_rows($get_login) == 1) {
			$glog = $db->fetch_array($get_login);
			
			setcookie ('login', $login_, $time);
			setcookie ('pass', $pass_, $time);
			
			if (file_exists ($_SERVER['DOCUMENT_ROOT'].'/explaylogs/admin_pan_log.txt')) {
				$file = file ($_SERVER['DOCUMENT_ROOT'].'/explaylogs/admin_pan_log.txt');
			} else {
				$f = fopen ($_SERVER['DOCUMENT_ROOT'].'/explaylogs/admin_pan_log.txt', 'w+');
				fclose ($f);
				$file = file ($_SERVER['DOCUMENT_ROOT'].'/explaylogs/admin_pan_log.txt');
			}	
			
			if (count ($file) > 40) {
				$new_file = fopen ($_SERVER['DOCUMENT_ROOT'].'/explaylogs/admin_pan_log.txt', 'w+');
				fclose ($new_file);
			}
			
			if ($file_log = fopen ($_SERVER['DOCUMENT_ROOT'].'/explaylogs/admin_pan_log.txt', 'w+')) {
				fwrite ($file_log, $login_.'<==>'.$_SERVER['REMOTE_ADDR'].'<==>'.time()."<==>true\n");
				fclose ($file_log);
			}
				
			big_message ('Привет, '.$glog['user_name'].'!','admin.php');
			exit;
			
		}
		else {
			$file = file ($_SERVER['DOCUMENT_ROOT'].'/explaylogs/admin_pan_log.txt');
			if (count ($file) > 40) {
				$new_file = fopen ($_SERVER['DOCUMENT_ROOT'].'/explaylogs/admin_pan_log.txt', 'w+');
				fclose ($new_file);
			}
			
			if ($file_log = fopen ($_SERVER['DOCUMENT_ROOT'].'/explaylogs/admin_pan_log.txt', 'w+')) {
				fwrite ($file_log, $login_.'<==>'.$_SERVER['REMOTE_ADDR'].'<==>'.time()."<==>false\n");
				fclose ($file_log);
			}
			
			print_big_authorization ('Вход с таким логином и паролем не возможен', 'index.php');
			exit;
		}
	}
}

// REGISTRATION

if (!is_admin()) {
	if ($_GET['admin_mod'] != 'enter') {
		print_big_authorization ('Авторизуйтесь, пожалуйста, как администратор', 'index.php');
		exit; 
	}
	else exit;
}


if (isset($_GET['name']))
	$mod_name = $_GET['name'];
else
	$mod_name = 'start';


if (isset($_GET['do']))
	$do = $_GET['do'] ;
else
	$do = '';



print_header ('Панель администратора', $SITE['site_name']);

include $_SERVER['DOCUMENT_ROOT'].'/themes/admin/header.php';

left_r1();

if (isset($_GET['do'])) {
	if (file_exists($_SERVER['DOCUMENT_ROOT']."/admin/$do.php"))
		include $_SERVER['DOCUMENT_ROOT']."/admin/$do.php";
	else {
		site_error ('Нет файла администрирования');
		come_back ('панели администратора', 'admin.php');
	}
}
elseif
	($mod_name == 'start') include $_SERVER['DOCUMENT_ROOT'].'/admin/panel.php';
else {
	$get_mod = $db->query ("SELECT module_admin FROM ".DB_PEREFIX."_modules WHERE module_name = '$mod_name'");
	
	$mod =$db->fetch_array ($get_mod);
	 
	if ($mod['module_admin'] != '') {
		if (file_exists($_SERVER['DOCUMENT_ROOT'].'/'.$mod['module_admin'])) include $_SERVER['DOCUMENT_ROOT'].'/'.$mod['module_admin'];
	 	else {
			site_error ('Нет файла администрирования');
			come_back ('панели администратора', 'admin.php');
		}
	}
	else site_error ('Управление модулем запрещено');
	
}

center_e ();

include $_SERVER['DOCUMENT_ROOT'].'/themes/admin/footer.php';

$end_time = microtime(); 
$end_array = explode(" ",$end_time); 
$end_time = $end_array[1] + $end_array[0]; 
$time = $end_time - $start_time; 
printf("\n<!-- page generated in %f sec. -->\n\n",$time);


ob_end_flush(); 
exit; 
